package com.itheima.utils;

/**
 * ClassName:TransactionManager
 * Package:com.itheima.utils
 * Description:事务管理相关的工具类，包含开启事务、提交事务commit、回滚事务rollback和释放连接
 * Date:2020/2/22 22:48
 * User: snow
 */
public class TransactionManager {
	private ConnectionUtils connectionUtils;

	public void setConnectionUtils(ConnectionUtils connectionUtils) {
		this.connectionUtils = connectionUtils;
	}

	/**
	 * 开启事务
	 */
	public void  beginTransaction(){
		try{
			System.out.println("事务。。。。connection begin");
			connectionUtils.getThreadConnection().setAutoCommit(false);
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 提交事务commit connection
	 */
	public void  commit(){
		try{
			System.out.println("事务。。。。connection commit");
			connectionUtils.getThreadConnection().commit();
		}catch (Exception e){
			e.printStackTrace();
		}

	}
	/**
	 * 回滚事务
	 */
	public void  rollback(){
		try{
			System.out.println("事务。。。。connection rollback");
			connectionUtils.getThreadConnection().rollback();
		}catch (Exception e){
			e.printStackTrace();
		}

	}
	/**
	 * 释放连接
	 */
	public void  release(){
		try{
			System.out.println("事务。。。。connection release");
			connectionUtils.getThreadConnection().close();//还回连接池
			connectionUtils.removeConnection();
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
